package com.lap.auth.adapter.driven.message;

import com.lap.auth.application.login.LoginLogAppService;
import com.lap.auth.application.login.command.CreateLoginLogCmd;
import com.lap.auth.domain.login.event.LoginLogEvent;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LoginLogListener {

  @Resource private LoginLogAppService loginLogAppService;

  @Async("loginLogExecutor")
  @EventListener
  public void handle(LoginLogEvent event) {
    CreateLoginLogCmd cmd = new CreateLoginLogCmd();
    cmd.setUserName(event.userName());
    cmd.setTimes(event.costMillis());
    cmd.setSuccess(event.success().getCode());
    cmd.setRemark(event.message());
    cmd.setIp(event.ip());
    cmd.setOs(event.os());
    cmd.setBrowser(event.browser());
    cmd.setDevice(event.device());
    loginLogAppService.createLoginLog(cmd);
  }
}
